<template>
	<view class="container">
		<view class="avatar" style="text-align: center;padding-top: 15px;">
			<!-- 上传组件 -->
			<span>商品分类图片:</span>
			<u-upload ref="img_logo" name="product_img" :maxCount="1" :fileList="filelist" width="100" height="100"
				@delete="del" @afterRead="read" />
		</view>

		<view class="secetion">
			<u-form labelPosition="left" labelWidth="60" :model="prolist" ref="product">


				<!-- 名称 -->
				<u-form-item label="名称">
					<u-input v-model="prolist.name" placeholder="请输入商品分类名称" />
				</u-form-item>
				<u-form-item label="门店" prop="store" borderBottom @click="showstore = true">
					<u-input v-model="prolist.store.name" disabled disabledColor="#ffffff" placeholder="请选择所属门店"
						border="none"></u-input>
					<u-icon slot="right" name="arrow-right"></u-icon>
				</u-form-item>


				<!-- 品牌描述 -->
				<u-form-item label="描述" borderBottom>
					<u-textarea placeholder="不低于3个字" v-model="prolist.content" count></u-textarea>
				</u-form-item>


				<!-- 提交按钮 -->
				<u-button text="添加商品分类" icon="pushpin" size="small" type="primary" @click="product" />
			</u-form>
			<u-popup :show="showstore" @close="closestore" @open="openstore">
				<view>
					<u-list height="560px">
						<u-list-item v-for="(item, index) in stolist" :key="index">
							<view class="branktypename" @click="storeSelect(item)">
								<u-cell :title="item.name">

								</u-cell>
							</view>
						</u-list-item>
					</u-list>
				</view>
			</u-popup>

		</view>
		<!-- 消息提醒 -->
		<u-toast ref="notice"></u-toast>
	</view>
</template>

<script>
export default {
	onLoad(options) {
		this.auth = this.$u.auth.check();

		this.ptypeid = options.ptypeid ? options.ptypeid : 0
		this.xzstoid = options.xzstoid ? options.xzstoid : this.auth.stoid


		this.current = options.current ? options.current : ""

		this.storelist()



	},


	created() {

		this.timer = setInterval(() => {

			let a = this.$u.count.check()

		}, 15000);

	},
	beforeDestroy() {
		clearInterval(this.timer)
	},
	data() {
		return {
			stolist:[],
			filelist: [],
			showstore: false,
			auth: {},
			stoid: null,
			xzstoid: 0,
			ptypeid: 0,
			current: "",
			newthumbArr:[],
			piclist: [],
			prolist: {
				name: "",
				content: "",
				thumb: "",
				store:{}
			},
			showbrank: false,
			rules: {
				content: {
					required: true,
					message: "不低于3个字",
					trigger: ["change"],
				},

				name: {
					required: true,
					message: "请填写商品分类名称",
					trigger: ["blur", "change"],
				},


			},

		};
	},

	methods: {
		async storelist() {
			let data = {
				adminid: this.auth.id,
				stoid: this.auth.stoid,
			};
			let result = await this.$u.api.product.storelist(data);

			if (result.code) {
				this.stolist = result.data;
			}
		},
		storeSelect(e) {
			this.prolist.store.name = e.name;
			this.stoid = e.id;
			this.showstore = false
		},
		openstore() {
			this.showstore = true;
			// console.log('close');
		},

		closestore() {
			this.showstore = false;
			// console.log('close');
		},

		async del(e) {

		
					let data = {
						thumb: e.file.url,
						ptypeid: this.ptypeid
					}
					
					var result = await this.$u.api.product.delprotypethumb(data)

					if (result.code) {
						this.$refs.notice.show({
							type: "success",
							message: result.msg,
							duration: 1000,
							complete: () => {
									this.filelist = []
									this.prolist.thumb = ""


							}
						});
						return false;
					}




			

		},
		async read(event) {

			//组装上传数据
			var data = {
				filePath: event.file.thumb,
				name: "product_img",
				header: {},

			};

			//调用接口
			var result = await this.$u.api.product.thumbs(data);

			if (result.code) {

				// 合并对象
				this.filelist = [{ url: result.data }]
				this.newthumbArr = this.newthumbArr.concat(result.data)



				this.prolist.thumb = result.data






			} else {
				this.$refs.notice.show({
					type: "error",
					message: result.msg,
				});
				return false;
			}
		},

		async product() {


			var data = {
				adminid: this.auth.id,
				name: this.prolist.name,
				xzstoid: this.xzstoid,
				thumb: this.prolist.thumb,
				content: this.prolist.content,
				newthumbArr:this.newthumbArr
				
			};


			let result = await this.$u.api.product.goodstypeadd(data);

			if (result.code) {
				this.$refs.notice.show({
					type: "success",
					message: result.msg,
					duration: 1200,
					complete: () => {
						//返回上一个界面
						uni.$u.route({
							type: "reLaunch",
							url: "/pagesC/product/goodstype/index",
							params: {
								current: this.current,
								ptype: result.data

							}
						})

					}
				});
			} else {
				this.$refs.notice.show({
					type: "error",
					message: result.msg,
					duration:1200
				});
			}




		},



	},
};
</script>

<style>
.secetion {
	padding: 20px;
}

.container {
	height: 100%;
	margin: 0 50rpx;
	box-sizing: border-box;
}

.u-avatar {
	margin: 80rpx auto 40rpx;
}

.vedio {
	text-align: center;
}

.u-vedio {
	margin: 80rpx auto 40rpx;
}

.xcx {
	width: 200rpx;
	height: 200rpx;
	margin: 80rpx auto 40rpx;
	border-radius: 100%;
	overflow: hidden;
}

.nickname {
	font-size: 14px;
	text-align: center;
}

.title {
	text-align: center;
	margin: 40rpx 0;
	font-size: 14rpx;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}

.vedio .u-upload {
	justify-content: center;
	align-items: center;
	margin: 0.3em auto;
}

.u-form-item__body__left__content__label.data-v-5e7216f1 {
	width: 75px;
}

.avatar .u-upload {
	justify-content: center;
	align-items: center;
	margin: 3em auto;
}
.branktypename{
	text-align: center;
  }
</style>